/*
 * timeOut.cpp
 *
 *  Created on: 25/07/2014
 *      Author: nicolas
 */

#include <sstream>
#include "../comun/TimeoutBroker.h"
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>

using namespace std;

int main(int argc, char **argv) {

	if (argc != 4) {
		perror("Args insuficientes");
		exit(1);
	}

	stringstream strValue;
	strValue << argv[1];

	unsigned int timeOutID;
	strValue >> timeOutID;

	int time = atoi(argv[2]);

	int brokerId = atoi(argv[3]);

	char encabezado[80];
	sprintf(encabezado, "TimeOut %i", timeOutID);

	openlog(encabezado, LOG_PID | LOG_CONS, LOG_LOCAL0);

	TimeoutBroker shmem(brokerId);

	sleep(time);

	syslog(LOG_INFO, "Se cumplio el timeout");

	if (shmem.get() == timeOutID) {
		syslog(LOG_INFO, "Enviando signal");
		kill(getppid(), SIGUSR1);
	}

}

